package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* SettingView2
* */
class SettingView2 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_FF80AB)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            val mStrokeWidth = (cx / 20).coerceAtLeast(minStrokeWidth)
            mPaint.strokeWidth = mStrokeWidth

            val offsetAngle1 = 15.0

            drawCircle(0F, 0F, cx * 5 / 14, mPaint)

            for (i in 1..pieces) {
                mPath.apply {
                    reset()

                    val baseValue10 =  cx * 10 / 11
                    val baseValue20 =  cx * 5 / 11

                    val radius1 = Math.toRadians(baseAngle.toDouble() * i - offsetAngle1)
                    val conValue10 = Math.cos(radius1).toFloat()
                    val sinValue10 = Math.sin(radius1).toFloat()
                    val con10 = baseValue10 * conValue10
                    val sin10 = baseValue10 * sinValue10
                    moveTo(con10, sin10)

                    val radius2 = Math.toRadians(baseAngle.toDouble() * i + offsetAngle1)
                    val conValue20 = Math.cos(radius2).toFloat()
                    val sinValue20 = Math.sin(radius2).toFloat()
                    val con20 = baseValue10 * conValue20
                    val sin20 = baseValue10 * sinValue20
                    lineTo(con20, sin20)

                    val radius3 = Math.toRadians(baseAngle.toDouble() * i + 30.0)
                    val conValue30 = Math.cos(radius3).toFloat()
                    val sinValue30 = Math.sin(radius3).toFloat()
                    val con30 = baseValue20 * conValue30
                    val sin30 = baseValue20 * sinValue30

                    val radius4 = Math.toRadians(baseAngle.toDouble() * (i + 1) - offsetAngle1)
                    val conValue40 = Math.cos(radius4).toFloat()
                    val sinValue40 = Math.sin(radius4).toFloat()
                    val con40 = baseValue10 * conValue40
                    val sin40 = baseValue10 * sinValue40

                    quadTo(con30, sin30, con40, sin40)
                }

                drawPath(mPath, mPaint)
            }

            restore()
        }
    }

    companion object {
        const val baseAngle = 60
        const val pieces = 6
    }
}